package cn.hyy.hrm.controller;

import cn.hyy.hrm.controller.vo.CourseVo;
import cn.hyy.hrm.doc.CourseDoucment;
import cn.hyy.hrm.service.ICourseService;
import cn.hyy.hrm.domain.Course;
import cn.hyy.hrm.query.CourseQuery;
import cn.hyy.basic.util.AjaxResult;
import cn.hyy.basic.util.PageList;
import cn.hyy.hrm.service.ICourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;
    @Autowired
    private ICourseTypeService courseTypeService;

    /**
    * 保存和修改公用的
    * @param course  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Course course){
        try {
            if(course.getId()!=null){
                courseService.updateById(course);
            }else{
                courseService.save(course);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            System.out.println(id);
            courseService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 根据id获取对象信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.getById(id);
    }


    /**
    * 查看所有的对象信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.list(null);
    }


    /**
     * 添加课程信息
     * @param vo
     * @return
     */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public AjaxResult add(@RequestBody CourseVo vo){
        try {
            if(vo.getId()!=null){
                courseService.update(vo);
            }else {
                courseService.add(vo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        System.out.println("=========>"+query);
        return courseService.getByQuery(query);
    }

    /**
     * 课程上线
     */
    @PostMapping("/onLine")
    public AjaxResult onLine(@RequestBody List<Long> ids){
        try {
            courseService.onLine(ids);
            return AjaxResult.me().setSuccess(true).setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败"+e.getMessage());
        }
    }
    /**
     * 课程下线
     * @param ids 前端参数  [1,2,3]
     * @return
     */
    @PostMapping("/offLine")
    public AjaxResult offLine(@RequestBody List<Long> ids){

        try {
            courseService.offLine(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下线失败!"+e.getMessage());
        }
    }

    /**
     * 课程页面静态化
     */
    @GetMapping("/staticHomePage")
    public AjaxResult staticHomePage(){
        try {
            courseTypeService.staticHomepage();
            return AjaxResult.me().setSuccess(true).setMessage("页面静态化成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("页面静态化失败"+e.getMessage());
        }
    }
    /**
     * 检索上线了的课程
     * @return
     */
    @PostMapping("/searchOnline")
    public PageList<CourseDoucment> searchOnline(@RequestBody CourseQuery courseQuery){
        return courseService.searchOnline(courseQuery);
    }

}
